import VueRouter from 'vue-router';

import index from '../view/front/index.vue';
import indexMain from '../view/front/component/index.vue';
import login from '../view/front/component/login.vue';
import register from '../view/front/component/register.vue';
import verification from '../view/front/component/verification.vue';
import fallbackPro from "../view/front/component/problem.vue";
import changePsd from "../view/front/component/changePsd.vue";

import main from '../view/back/index.vue';
import bIndex from '../view/back/bIndex.vue';
import publicPage from '../view/back/glossary/publicPage.vue';
import glossaryList from '../view/back/glossary/component/list.vue';
import glossaryDetail from '../view/back/glossary/component/detail.vue';
import worksMain from '../view/back/worksManage/worksMain.vue';
import worksList from '../view/back/worksManage/component/worksList.vue';
import worksAdd from '../view/back/worksManage/component/worksAdd.vue';
import programList from "../view/back/worksManage/component/programList.vue";
import programAdd from "../view/back/worksManage/component/programAdd.vue";
import programDetail from '../view/back/worksManage/component/detail.vue'

import userManage from '../view/back/systemConfig/userManage.vue';
import roleManage from '../view/back/systemConfig/roleManage.vue';
import unitManage from '../view/back/systemConfig/unitManage.vue';
import authManage from '../view/back/systemConfig/authManage.vue';
import menuManage from '../view/back/systemConfig/menuManage.vue';
import checkRoleChange from '../view/back/systemConfig/checkChangeRole.vue';
import checkAgency from '../view/back/systemConfig/checkAgencyManage.vue';
import sysLogManage from "../view/back/systemConfig/sysLogManage.vue";
import problemManage from "../view/back/systemConfig/problemManage.vue";

import dictManage from '../view/back/systemConfig/dictManage.vue';
import noun  from '../view/back/systemConfig/glossary.vue';

import userInfo from '../view/back/userInfo.vue'

// 3. 创建路由对象
var router = new VueRouter({
  routes: [
    {path:"/",redirect:"/index"},
    {path:"/index",component:index,children:[
      {path:"",component:indexMain},
      {path:"/login",component:login},
      {path:"/register",component:register},
      {path:"/verification",component:verification},
      {path:"/fallbackPro",component:fallbackPro},
      {path:"/changePsd",component:changePsd},

    ]},
    {path:"/backIndex",component:main,children:[
      {path: '', redirect: "/backIndex2" },
      {path:"/backIndex2",component:bIndex},
      {path:"/common",component:publicPage,children:[
        {path:"",name:"commonPage",component:glossaryList},
        {path:"/glossaryList",component:glossaryList},
        {path:"/glossaryDetail",name:"glossaryDetail",component:glossaryDetail},
        {path:"/worksList",component:worksList},
        {path:"/worksAdd",component:worksAdd},
        // {path:"/worksDetail/:id",component:worksAdd},
        {path:"/worksDetail",name: 'worksDetail',component:worksAdd},
        {path:"/programList",component:programList},
        {path:"/programAdd",name:'programAdd',component:programAdd},
        {path:"/programDetail/:id",component:programAdd},
        // 用户信息
        {path:"/userInfo",component:userInfo},

      ]},
      {path:"/FDetail",name:'FDetail',component:programDetail},
      {path:"/user",component:userManage},
      {path:"/role",component:roleManage},
      {path:"/unit",component:unitManage},
      {path:"/auth",component:authManage},
      {path:"/menu",component:menuManage},
      {path:"/checkRole",component:checkRoleChange},
      {path:"/checkAgency",component:checkAgency},
      {path:'/sysLog',component:sysLogManage},
      {path:'/problem',component:problemManage},
      {path:"/dict",component:dictManage,children:[
        // {path:"",component:dictData,name:dictData},
      ]},
      {path:"/noun",component:noun},
    ]}
  ],
  // linkActiveClass:'active-btn'
})

// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/verification' || to.path === '/register' || to.path === '/index' || to.path === '/fallbackPro' || to.path === "/changePsd") {
    next();
  } else {
    let token = localStorage.getItem('token');
    if (token === null || token === 'null' || token === '' || token === "undefined" || token === undefined) {
      this.$message({center: true,message: "请先登陆",type: 'warning'});
      next('/login');
    } else {
      var menuList = sessionStorage.menuList?JSON.parse(sessionStorage.menuList):[]
      var matchMenu = ""
      // 防止没有权限的用户直接访问路径
      menuList.forEach(element => {
        if(element.url == to.path){
          matchMenu = element
        }
        if(element.secMenu && element.secMenu.length>0){
          element.secMenu.forEach(el => {
            if(el.url == to.path){
              matchMenu = el
            }
          })
        }
      })
      
      if(matchMenu.status == 0){
        this.$message({center: true,message: "您没有访问权限，请重新登陆",type: 'warning'});
        next('/login');
      }else{
        next();
      }
    }
  }
});

// 把路由对象暴露出去
export default router